package com.the4man.cnbdw.listener.socket;

import com.the4man.cnbdw.cache.DeviceCache;
import com.the4man.framework.model.PointInfo;
import com.the4man.framework.util.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Date;
import java.util.UUID;

/**
 * Created by lenovo on 2017/12/12.
 */
public class UdpSocketOperate extends Thread {
    private Logger log = LoggerFactory.getLogger(UdpSocketOperate.class);
    private byte[] left;
    private byte[] bytes;
    private DeviceCache deviceCache;

    public UdpSocketOperate(byte[] bytes, DeviceCache deviceCache) {
        this.bytes = bytes;
        this.deviceCache = deviceCache;
    }

    public void run() {
        try {
            System.out.println("UdpSocke1112222tOperate--- r11un()");
//            byte[] tmp = null;
//            byte[] temp = null;
            byte[] temp = Arrays.copyOf(this.bytes, this.bytes.length);
            byte[] ab = new byte[]{temp[4]};
            String cc = byte2HexStr(ab);
            String apMac;
            String eqMac;
            String rssi;
//            int j;
            byte[] tmp;
            int j;
            if(cc.equals("D8")) {
                System.out.println("111111");
                apMac = null;
                eqMac = null;
                rssi = null;
                byte[] num = new byte[]{temp[8]};
                j = Integer.valueOf(byte2HexStr(num), 16).intValue();
                tmp = Arrays.copyOf(this.bytes, 12 + 48 * j);
                int a = tmp.length;

                label245:
                for(int i = 0; i < tmp.length; ++i) {
                    if(tmp.length - i < 48) {
                        this.left = new byte[tmp.length - i];
                        j = 0;

                        while(true) {
                            if(j >= this.left.length) {
                                break label245;
                            }

                            this.left[j] = tmp[i + j];
                            ++j;
                        }
                    }

                    if(tmp[i] == -52 && tmp[i + 1] == -125 && tmp[i + 4] == -42) {
                        byte[] apMac_bs = new byte[6];
                        byte[] eqMac_bs = new byte[6];
                        byte[] rssi_bs = new byte[1];

                        for(int k = 0; k < 6; ++k) {
                            apMac_bs[k] = tmp[i + 8 + k];
                            eqMac_bs[k] = tmp[i + 16 + k];
                        }

                        rssi_bs[0] = tmp[i + 32];
                        rssi = byte2HexStr(rssi_bs);
                        apMac = byte2HexStr(apMac_bs);
                        eqMac = byte2HexStr(eqMac_bs);

                        try {
                            String apMac1 = StringUtils.contains(apMac, "-")?apMac:spl(apMac);
                            String eqMac1 = StringUtils.contains(eqMac, "-")?eqMac:spl(eqMac);
                            if(this.deviceCache.contains(eqMac1)) {
                                this.log.info("apmac:{}, eqmac:{}", apMac, eqMac1);
                                PointInfo point = new PointInfo();
                                point.setId(UUID.randomUUID().toString());
                                point.setApMac(apMac1);
                                point.setRssi("" + Integer.valueOf(rssi, 16));
                                point.setDeviceMac(eqMac1);
                                point.setCreateTime(DateUtil.getInstance().formatDate(new Date()));
                                point.setUpdateTime(DateUtil.getInstance().formatDate(new Date()));
                                this.deviceCache.putPoint(point);
                            } else {
                                this.log.info("mac does not ignore the existence of Operation,apmac:{}, eqmac:{}", apMac, eqMac);
                            }
                        } catch (Exception var24) {
                            var24.printStackTrace();
                        }

                        i += 47;
                    }
                }
            } else if(cc.equals("D6")) {
                apMac = null;
                eqMac = null;
                rssi = null;
//                int count = true;
                tmp = Arrays.copyOf(this.bytes, 48);
                if(tmp.length < 48) {
                    this.left = new byte[tmp.length];

                    for(j = 0; j < this.left.length; ++j) {
                        this.left[j] = tmp[j];
                    }
                } else if(tmp[0] == -52 && tmp[1] == -125 && tmp[4] == -42) {
                    byte[] apMac_bs = new byte[6];
                    byte[] eqMac_bs = new byte[6];
                    byte[] rssi_bs = new byte[1];

                    for(j = 0; j < 6; ++j) {
                        apMac_bs[j] = tmp[8 + j];
                        eqMac_bs[j] = tmp[16 + j];
                    }

                    rssi_bs[0] = tmp[32];
                    rssi = byte2HexStr(rssi_bs);
                    apMac = byte2HexStr(apMac_bs);
                    eqMac = byte2HexStr(eqMac_bs);

                    try {
                        String apMac1 = StringUtils.contains(apMac, "-")?apMac:spl(apMac);
                        String eqMac1 = StringUtils.contains(eqMac, "-")?eqMac:spl(eqMac);
                        if(this.deviceCache.contains(eqMac1)) {
                            this.log.info("apmac:{}, eqmac:{}", apMac, eqMac1);
                            PointInfo point = new PointInfo();
                            point.setId(UUID.randomUUID().toString());
                            point.setApMac(apMac1);
                            point.setRssi("" + Integer.valueOf(rssi, 16));
                            point.setDeviceMac(eqMac1);
                            point.setCreateTime(DateUtil.getInstance().formatDate(new Date()));
                            point.setUpdateTime(DateUtil.getInstance().formatDate(new Date()));
                            this.deviceCache.putPoint(point);
                        } else {
                            this.log.info("mac does not ignore the existence of Operation,apmac:{}, eqmac:{}", apMac, eqMac);
                        }
                    } catch (Exception var23) {
                        var23.printStackTrace();
                    }
                }
            }

            this.log.info("-------------------------------------- finish.....");
        } catch (Exception var25) {
            var25.printStackTrace();
            this.log.error("exception");
        } finally {
            this.log.info("UdpSocketOperate finish.....");
        }

    }

    public static byte[] toStringHex(String s) {
        byte[] baKeyword = new byte[s.length() / 2];

        for(int i = 0; i < baKeyword.length; ++i) {
            try {
                baKeyword[i] = (byte)(255 & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception var4) {
                var4.printStackTrace();
            }
        }

        return baKeyword;
    }

    public static String byte2HexStr(byte[] b) {
        String hs = "";
        String stmp = "";

        for(int n = 0; n < b.length; ++n) {
            stmp = Integer.toHexString(b[n] & 255);
            if(stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }

        return hs.toUpperCase();
    }

    public static byte[] hexStr2Bytes(String src) {
//        int m = false;
//        int n = false;
        int l = src.length() / 2;
        byte[] ret = new byte[l];

        for(int i = 0; i < l; ++i) {
            int m = i * 2 + 1;
            int n = m + 1;
            ret[i] = uniteBytes(src.substring(i * 2, m), src.substring(m, n));
        }

        return ret;
    }

    private static byte uniteBytes(String src0, String src1) {
        byte b0 = Byte.decode("0x" + src0).byteValue();
        b0 = (byte)(b0 << 4);
        byte b1 = Byte.decode("0x" + src1).byteValue();
        byte ret = (byte)(b0 | b1);
        return ret;
    }

    private static String spl(String s) {
        StringBuffer s1 = new StringBuffer(s);

        for(int index = 2; index < s1.length(); index += 3) {
            s1.insert(index, '-');
        }

        return s1.toString();
    }
}
